﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.IO;
using School.Entities;
using School.Common;
using System.Data.Entity;
using School.Admission;

namespace School.Pages.Admission
{
    public partial class Transfer_Out : System.Web.UI.Page
    {
        //***************** This is function to handle page when page load ***************************
        //-**********************************************************************
        DataTable dt = null;
        int i = 1;

        Int64 mytrid;

        protected void Page_Load(object sender, EventArgs e)
        {
            if ((IQueryable<int>)Session["privilage"] != null)
            {
                var x = (IQueryable<int>)Session["privilage"];

                if (x.Contains(1021) == true)
                {

                    if (!(Page.IsPostBack))
                    {
                        //security


                        //fill crls

                        Fillcrls();

                        InitializeControls();

                        GridDataBind();

                    }

                }
                else
                    Response.Redirect("../organization/no_privelege.aspx");
            }
            else
                Response.Redirect("../organization/no_privelege.aspx");
        }
        # region "Collect Data"

        //***************** This is function to bind data grid  ***************************
        //-**************************************************************************
        protected void GridDataBind()
        {
            Int32 mycount;
            Hashtable SearchTable = new Hashtable();
            long campus = 0;
            long certificate = 0;
            long stage = 0;
            long grade = 0;
            string name = txtSearchName.Text;

            /*if (txtSearchName.Text.Length > 0)
            {

                SearchTable.Add("SearchType", rblSearchType.SelectedValue);
                
                SearchTable.Add("SD_FullName", txtSearchName.Text.ToString());
            }
            /*if (ddlSearchcampus_Id.SelectedIndex > 0)
            {
                SearchTable.Add("Campus_Id", ddlSearchcampus_Id.SelectedIndex.ToString());
            }

            if (ddlSearchCertificate_Id.SelectedIndex > 0)
            {
                SearchTable.Add("Certificate_Id", ddlSearchCertificate_Id.SelectedIndex.ToString());
            }
            if (ddlSearchCertificate_Id.SelectedIndex > 0)
            {
                SearchTable.Add("NotStatus_Id", "9");
            }
            if (ddlSearchStage_Id.SelectedIndex > 0)
            {
                SearchTable.Add("Stage_Id", ddlSearchCertificate_Id.SelectedIndex.ToString());
            }
            if (ddlSearchGrade_ID.SelectedIndex > 0)
            {
                SearchTable.Add("Grade_ID", ddlSearchGrade_ID.SelectedValue.ToString());
            }*/

            long.TryParse(ddlSearchcampus_Id.SelectedValue, out campus);
            long.TryParse(ddlSearchCertificate_Id.SelectedValue, out certificate);
            long.TryParse(ddlSearchStage_Id.SelectedValue, out stage);
            long.TryParse(ddlSearchGrade_ID.SelectedValue, out grade);
            
            var data = Students.SearchFiltered(campus, certificate, stage, grade, name);

            griddata.DataSource = data;
            griddata.DataBind();
            griddata.PageIndex = 0;
            griddata.Visible = true;
            mycount = griddata.Rows.Count;

            lblRecordNo.Text = "عدد نتائج البحث = " + data.Count().ToString();
            if (mycount == 0)
            {
                lblMessage.Text = "لا يوجد بيانات";
            }
            else
            {
                lblMessage.Text = "";
            }            

        }

        # endregion


        # region "Fill all crls"

        //***************** This is function to fill crl  ***************************
        //-**************************************************************************

        protected void Fillcrls()
        {



            ddlSearchcampus_Id.DataSource = Structure.GetCampuses();
            ddlSearchcampus_Id.DataTextField = "Name";
            ddlSearchcampus_Id.DataValueField = "Id";
            ddlSearchcampus_Id.DataBind();
            ddlSearchcampus_Id.Items.Insert(0, new ListItem("اختيار", "-1"));

        }


        //***************** This is function to initlaize crl  ***************************
        //-**************************************************************************
        protected void InitializeControls()
        {
            ddlSearchcampus_Id.SelectedIndex = 0;
            ddlSearchCertificate_Id.SelectedIndex = 0;
            ddlSearchGrade_ID.SelectedIndex = 0;
            ddlSearchStage_Id.SelectedIndex = 0;
        }

        # endregion


        # region "Mode Handlers"

        //***************** This is function to handle page in add mode  ***************************
        //-**************************************************************************
        protected void PutFormInAddMode()
        {
            txtSearchName.Text = "";
        }


        //***************** This is function to handle page in brows mode  ***************************
        //-**************************************************************************

        protected void PutFormInBrwosMode()
        {
            pnlbrows.Visible = false;
        }

        #endregion


        # region "Grid Events ***********"

        //***************** This is function to handle page when Page Index Changing   ***************************
        //-**************************************************************************

        protected void griddata_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            griddata.PageIndex = e.NewPageIndex;
            GridDataBind();
        }


        //***************** This is function to handle page when Row Deleting  ***************************
        //-**************************************************************************

        protected void griddata_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                griddata.SelectedIndex = e.RowIndex;

                long TRID = Int32.Parse(griddata.SelectedDataKey.Value.ToString());
                Boolean successadd = FileOrder.Delete(TRID);
                if (successadd == true)
                {
                    GridDataBind();
                    lblMessage.Visible = true;
                    lblMessage.Text = "Record(s) was deleted successfully";

                }
            }
            catch (Exception)
            {
                lblMessage.Text = "Record(s) cannot be deleted, please contact your administrator";

            }

        }


        //***************** This is function to handle page when Row Data Bound  ***************************
        //-**************************************************************************
        protected void griddata_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton LB = (LinkButton)e.Row.FindControl("LinkButton1");
                LB.Attributes.Add("onclick", "javascript:return " +
                "confirm('Are you sure you want to delete record(s) ( " +
                DataBinder.Eval(e.Row.DataItem, "CampusName") + " ) ?')");

            }
        }

        protected void griddata_SelectedIndexChanged(object sender, EventArgs e)
        {
            pnlbrows.Visible = false;
            DetailsView1bind();

        }

        protected void DetailsView1bind()
        {

        }
        # endregion

        # region   "Execute commands sent to the form"



        //***************** This is function to handle page when Puser click in الغاء btn  ***************************
        //-**************************************************************************
        protected void btnCancel_Click(object sender, EventArgs e)
        {


        }


        //***************** This is function to handle page when Puser click in اضافة btn  ***************************
        //-**************************************************************************

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            
        }

        //***************** This is function to handle page when Puser click in انهاء btn  ***************************
        //-**************************************************************************


        protected void btnend_Click(object sender, EventArgs e)
        {
            btnAdd.Visible = true;
            pnlbrows.Visible = true;
        }
        //***************** This is function to handle page when Puser click in حفظ btn  ***************************
        //-**************************************************************************
        # endregion
        # region " Search *********************"

        //***************** This is function to search by  Get All btn ***************************
        //-**************************************************************************
        protected void btnGetAll_Click(object sender, EventArgs e)
        {
            InitializeControls();

            GridDataBind();
        }



        protected void btnSearch_Click(object sender, EventArgs e)
        {
            GridDataBind();
        }

        # endregion

        protected void btnAccept_Click(object sender, EventArgs e)
        {
            AcceptOfStudents();
        }

        protected void AcceptOfStudents()
        {

            int i;
            for (i = 0; (i <= (griddata.Rows.Count - 1)); i++)
            {
                if (griddata.Rows[i].Visible == true)
                {
                    CheckBox AcceptStudent;
                    AcceptStudent = ((CheckBox)(griddata.Rows[i].FindControl("CheckBox1")));

                    if ((AcceptStudent.Checked == true))
                    {

                        Int64 trid;
                        Label lbltrid;
                        lbltrid = ((Label)(griddata.Rows[i].FindControl("lbltrid")));
                        trid = Convert.ToInt64(lbltrid.Text);

                        Student.UpdateStatusId(new TE_Student
                        {
                            Id = trid,
                            Status_Id = 9
                        }
                        );
                        TE_Student mystudent = Student.Single(trid);

                        if (StudentTransaction.Add(new ADM_Student_Transaction
                        {
                            Student_Id = trid,
                            Status_Id = 9,
                            EductionalYear_Id =long.Parse(Structure.GetCurrentEductionalYear().ElementAt(0).Id.ToString() ),
                            Campus_Id = mystudent.Campus_Id,
                            Certificate_Id = mystudent.Certificate_Id,
                            Stage_Id = mystudent.Stage_Id,
                            Grad_Id = mystudent.Grade_ID
                        }) != 0)
                        {
                            lblMessage.Text = "تم تغيير حالة الطالب بنجاح";
                        }
                        else
                        {
                            lblMessage.Text = "لم يتم تغيير حالة الطالب";
                        }
                        ;

                        griddata.Rows[i].Visible = false;

                    }
                }

            }


        }


        protected void ddlSearchcampus_Id_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlSearchcampus_Id.SelectedIndex > 0)
            {
                ddlSearchCertificate_Id.Items.Clear();
                ddlSearchGrade_ID.Items.Clear();
                ddlSearchStage_Id.Items.Clear();
                ddlSearchCertificate_Id.DataSource = Structure.GetCertificatesByCampus(Convert.ToInt64( ddlSearchcampus_Id.SelectedValue));
                ddlSearchCertificate_Id.DataTextField = "Name";
                ddlSearchCertificate_Id.DataValueField = "Id";
                ddlSearchCertificate_Id.DataBind();
                ddlSearchCertificate_Id.Items.Insert(0, new ListItem("اختيار", "-1"));
                ddlSearchCertificate_Id.Enabled = true;
                ddlSearchGrade_ID.Enabled = false;
                ddlSearchStage_Id.Enabled = false;
            }
        }

        protected void ddlSearchCertificate_Id_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlSearchCertificate_Id.SelectedIndex > 0)
            {
                ddlSearchStage_Id.Items.Clear();
                ddlSearchGrade_ID.Items.Clear();
                ddlSearchStage_Id.DataSource = Structure.GetStagesByCertificate(Convert.ToInt64(ddlSearchCertificate_Id.SelectedValue));
                ddlSearchStage_Id.DataTextField = "Name";
                ddlSearchStage_Id.DataValueField = "id";
                ddlSearchStage_Id.DataBind();
                ddlSearchStage_Id.Items.Insert(0, new ListItem("اختيار", "-1"));
                ddlSearchStage_Id.Enabled = true;
                ddlSearchGrade_ID.Enabled = false;


            }
        }

        protected void ddlSearchStage_Id_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlSearchStage_Id.SelectedIndex > 0)
            {

                ddlSearchGrade_ID.Items.Clear();
                ddlSearchGrade_ID.DataSource = Structure.GetGradesByStage(Convert.ToInt64(ddlSearchStage_Id.SelectedValue ));
                ddlSearchGrade_ID.DataTextField = "Name";
                ddlSearchGrade_ID.DataValueField = "id";
                ddlSearchGrade_ID.DataBind();
                ddlSearchGrade_ID.Items.Insert(0, new ListItem("اختيار", "-1"));
                ddlSearchGrade_ID.Enabled = true;


            }
        }

     

    }
}